
# generate table F6

# load packages
library(rio) # load data
library(tidyverse) # data manipulation
library(survival) # estimate survival models
library(stargazer) # generate tables

# set working directory
setwd("~/replication_files/")

# load data for analysis
data_with_dictionary <- import("data/full_data.csv") %>%
  mutate(across(c(imf_program,field_discovery,grad_school_econ_usa,iso3c,year), as.factor)) # treat factors as factors


survival_data <- data_with_dictionary %>%
  group_by(iso3c) %>%
  mutate(start2 = 1:length(iso3c),
         start = start2 - 1,
         stop = start + 1) %>%
  ungroup() %>%
  # omit countries with unobserved histories
  filter(previous_policy != 1 & iso3c != "DZA" & iso3c != "AZE" & iso3c != "BWA" & 
           iso3c != "TCD" & iso3c != "CHL" & iso3c != "ECU" & iso3c != "GAB" & 
           iso3c != "IRN" & iso3c != "KAZ" & iso3c != "MYS" & iso3c != "MEX" & 
           iso3c != "NAM" & iso3c != "PNG" & iso3c != "PER" & iso3c != "RUS")

cox1 <- coxph(Surv(start, stop, policy_passage) ~ resource_mentions_absolute + 
                grad_school_econ_usa + fdi_performance_index_lag +
                imf_program + 
                resource_rents_lag + log_gdp_per_capita_lag + gdp_growth_lag + field_discovery_lag + 
                polyarchy + left_executive + protest,
              data = survival_data)

cox2 <- coxph(Surv(start, stop, policy_passage) ~ resource_mentions_tfidf + 
                grad_school_econ_usa + fdi_performance_index_lag +
                imf_program + 
                resource_rents_lag + log_gdp_per_capita_lag + gdp_growth_lag + field_discovery_lag + 
                polyarchy + left_executive + protest,
              data = survival_data)

stargazer(cox1,cox2, # type="text", 
          omit = c("iso3c","stop"), dep.var.labels = "Time to Policy Passage",
          covariate.labels = c("Natural Resource Term Frequency","Natural Resource Term Frequency (TF--IDF)",
                               "Technocratic Finance Minister = 1", "FDI Performance Index",
                               "IMF Program = 1",
                               "Resource Rents, t--1", "Log GDP Per Capita, t--1", "GDP Growth, t--1", "Field Discovery = 1, t--1",
                               "Polyarchy", "Left Executive", "Protest Count"),
          keep.stat = c("n", "rsq", "ll", "logrank", "wald"))




